import Vue from 'vue'
import VueRouter from "vue-router"

Vue.use(VueRouter); 
import SVG from '../page/svg'
import Home from '../page/home' 
import Login from '../page/login';
import User from '../components/menu/user';
import Roles from '../components/menu/roles';
import Rights from '../components/menu/rights';

import Goods from '../components/goods/Goods'; 
import Params from '../components/goods/Params'; 
import Cate from '../components/goods/Cate'; 

var router = new VueRouter({
    mode: "history",
    routes: [
        
        // {
        //     path:'/',
        //     component:SVG
        // },
        {
            path:"/",
            component:Home,
            redirect:"/svg",
            children:[
                {
                    path:"svg",
                    component:SVG
                },
                {
                    path:"users",
                    component:User
                },
                {
                    path:"roles",
                    component:Roles
                },
                {
                    path:"rights",
                    component:Rights 
                },
                {
                    path:"goods",
                    component:Goods
                },
                {
                    path:"params",
                    component:Params
                },
                {
                    path:"categories",
                    component:Cate
                }
            ]

        },
        {
            path:'/login',
            component:Login
        }
    ]
});


//挂载路由导航守卫(判断是否登录)
router.beforeEach( (to,from,next) => {
    //to 要跳转到的页面
    //from  来自哪个页面
    //next() 直接放行    next(path) 拦截并跳转到指定路径

    if(to.path === "/login")
    {
       return next();
    }

   const token = window.sessionStorage.getItem("token")
    if( !token)
    {
        return next("/login");
    }
    next();

})


export default router;